<template>
  <div class="home">
    <!-- 顶部 -->
    <div class="home-header">
      <div>
        <span>你好</span><span class="username" @click="toMy">{{ userName }}</span>
      </div>
      <div class="search">
        <van-search
          v-model="SearchValue"
          placeholder="请输入搜索关键词"
          shape="round"
          @focus="toSearch"
        />
      </div>
    </div>

    <!-- 轮播图 -->
    <div class="swiper">
      <van-swipe :autoplay="2000">
        <van-swipe-item v-for="(image, index) in banner" :key="index">
          <img v-lazy="image.bannerImg" @click="todetail(image.pid)"/>
        </van-swipe-item>
      </van-swipe>
    </div>

    <!-- 热卖推荐 -->
    <div class="hot-commond">
      <div>热卖推荐</div>
    </div>
    <!-- 商品 --> 
    <div class="product-box">
        <div class="product-item"  v-for="(item,index) in isHot" :key="index" @click="todetail(item.pid)">
          <product :product='item' />
        </div>
    </div>
    <!-- 占位 底部导航的 -->
    <div class="zanwei"></div>
  </div>
</template>

<script>
import Product from '../components/Product.vue'
export default {
  name: "Home",
  data() {
    return {
      SearchValue: "",
      userName: "",
      banner:[],
      isHot:[]
    };
  },
  components: {Product},
  created() {
    this.getUsername();
    this.getBanner();
    this.getHot();
  },
  methods: {
    toSearch(){
       this.$router.push({name:'Search'})
    },
    toMy(){
      this.$router.push({name:'My'})
    },
    // 获取用户名
    getUsername() {
      if (!sessionStorage.getItem("token")) {
        this.userName = "请先登录！";
      }
      this.axios({
        method: "get",
        url: "/findAccountInfo",
      })
        .then((res) => {
          console.log("个人资料res=>", res);
          this.userName = res.data.result[0].nickName;
        })
        .catch((err) => {
          console.log("err", err);
        });
    },
    // 获取轮播图
    getBanner(){
        this.axios({
        method: "get",
        url: "/banner",
      })
        .then((res) => {
          console.log("banner res=>", res);
          this.banner=res.data.result
          console.log(this.banner);
        })
        .catch((err) => {
          console.log("err", err);
        });
    },
    // 轮播图点击跳转详情
    todetail(id){
        this.$router.push({name:'Detail',params:{pID:id}})
    },
    // 获取热卖推荐
    getHot(){
        this.axios({
        method: "get",
        url: "/typeProducts",
        params:{
          key:'isHot',
          value: 1
        }
      })
        .then((res) => {
          console.log("isHot res=>", res);
          if(res.data.code===500){
              this.isHot=res.data.result
              console.log(this.isHot);
          }
        })
        .catch((err) => {
          console.log("err", err);
        });
    }
  },
};
</script>
<style lang="less" scoped>
// 商品
.product-box{
  width: 355px;
  margin: 0 auto;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-around;
  .product-item{
    width: 150px;
    padding: 10px;
    margin-bottom: 10px;
    background-color: #fff;
    border-radius: 10px;
  }
  .product{
    width: 150px;
    margin: 0 auto;
   
    // overflow: hidden;
    /deep/img{
       border-radius: 10px;
    }
    /deep/.product-zhname{
      margin-top: 40px;
    }
  }
}
// 热卖推荐
.hot-commond{
  width: 355px;
  height: 40px;
  margin: 0 auto;
  margin-bottom: 10px;
  background-color: #fff;
  display: flex;
  align-items: center;
  div{
    height: 30px;
    line-height: 30px;
    font-size: 14px;
    width: 80px;
    border-top-right-radius: 20px;
    background-color: #0c34ba;
    color: #fff;
    text-align: center;
  }
}
// 轮播图
.swiper{
  width: 355px;
  margin: 10px auto;
  // margin-bottom: 0px;
  border-radius: 5% !important;
  overflow: hidden;
  img{
    display: block;
    width: 355px;
    height: 265px;
  }

}
// 顶部
.home-header {
  display: flex;
  align-items: center;
  background-color: #fff;
  border-bottom: 1px solid rgb(223, 221, 221);
  & div:first-child {
    flex: 1;
    span:first-child {
      font-size: 16px;
      font-weight: bold;
      margin-left: 10px;
      color: #666;
    }
    .username {
      width: 65px;
      font-size: 16px;
      font-weight: bold;
      color: #0c34ba;
      margin-left: 5px;
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
    }
  }
  & div:last-child {
    flex: 2;
  }
}
// 搜索
.search {
  position: sticky;
  top: 0;
  border-bottom: 0px;
}
.home{
  width: 100%;
  background-color: #f7f7f7;
}
.zanwei{
  width: 100%;
  height: 50px;
}
</style>
